import { observable, action } from 'mobx';
import * as Apis from "../../api/customerApis";
import { Toast, Bubble, Tools } from 'weaver-mobile';
import { ListViewPage,RouteLayout ,Share} from 'weaver-mobile-page';
import * as adapter from '../../constants/adapter';
const getLabel = Tools.getLabel;
export default class ContacterCardStore {
  comObj = {};
  params = {};
  @observable tabs = [
    { title: getLabel('2121','详细信息'), key: 'info' },
    { title: getLabel('17532','联系记录'), key: 'contact' }
  ];
  @observable fieldItems = [];
  @observable id = "";
  @observable customerId = "";
  @observable title1 = "";
  @observable title2 = "";
  @observable title3 = "";
  @observable concerned = false;
  @observable canView = false;
  @observable canEdit = false;
  @observable initResponsed = false;// 是否已经得到初始化数据的响应
  @observable initSuccessed = false;// 初始化数据是否获取成功
  @observable initFailedMsg = "";   // 初始化处理失败的原因
  @observable tabPage = "info";
  @observable refreshing = false;
  // list
  @observable contactListStore = {};
  // layout
  @observable customerCardLayout = {};
  @observable customerMapLayout = {};
  @observable contacterViewAndEditLayout = {};


  @observable canShare = false;
  @observable shareContacterLayout = {};
  @observable shareStore = {};
  @observable userId = "";
  @observable onlyView = false;



  @action
  doNew = (comObj,params) =>{
    this.comObj = comObj;
    this.params = params;
    window.crmContacter=this;
    // console.log(comObj);
  }

  @action
  doInit = () =>{
    let params = this.params;
    this.refreshing = true;
    Apis.getContacterCard(params).then(data => {       
      this.doInitCallBack(params,data);
    });
  }

  @action
  doInitCallBack = (params,data) =>{
    this.refreshing = false;
    this.initResponsed = true;
    if(data.status == "0"){
      this.initSuccessed = false;
      this.initFailedMsg = data.errorMsg;
    }else{
      const currentPath = this.comObj.props.location.pathname;
      this.contactListStore = new ListViewPage.Store({ route: currentPath,id:"contactListStore" });
      this.customerCardLayout = new RouteLayout.Store({route:currentPath , id:'customerCardLayout'});
      this.customerMapLayout = new RouteLayout.Store({route:currentPath , id:'customerMapLayout'});
      this.contacterViewAndEditLayout = new RouteLayout.Store({route:currentPath , id:'contacterViewAndEditLayout'});

      this.shareContacterLayout = new RouteLayout.Store({ route: currentPath, id: 'shareContacterLayout' });
      this.shareStore = new Share.Store({ route: currentPath+'/shareContacterLayout', id: 'share' });
   


      this.initSuccessed = true;
      this.canEdit = data.canEdit;
      this.canView = data.canView;

      this.canShare = data.canShare;
      this.userId = data.loginid;
      this.onlyView = data.onlyView;

      this.fieldItems = data.contacterInfo;
      this.id = data.id;
      this.customerId = data.customerId;
      this.title1 = data.title1;
      this.title2 = data.title2;
      this.title3 = data.title3;
      this.concerned = data.concerned;
    }
  }

  @action
  doClear = () =>{
      this.initResponsed = false;
      this.initSuccessed = false;
      this.initFailedMsg = "";
      this.tabPage = "info";
  }

  @action
  setConcerned(concerned){
    this.concerned = !concerned;
    let params = {relatedIds:this.id,important:concerned ? 0 : 1};
    Apis.setContacterConcerned(params).then(data => {       
      Bubble.success(getLabel('83551',"保存成功"), 1, {backgroundColor:adapter.BUBBLE_BGCOLOR},2,()=>{
        if(window.crmGlobal && window.crmGlobal.listTabIndex == "attention"){
          window.crmGlobal.doSearch();
        }
      });      
    });
  }

  @action
  deleteContacter(){
    let params = {contacterId:this.id};
    Apis.deleteContacter(params).then(data => { 
      if(data.status == "success"){
        Bubble.success(getLabel('83472', '删除成功!'), 1, {backgroundColor:adapter.BUBBLE_BGCOLOR}, 2,()=>{
          this.comObj.props.history.go(-1);
          if(typeof(this.comObj.props.cardStore!=="undefined")&&this.comObj.props.cardStore){
            this.comObj.props.cardStore.doInit();
        }
          window.crmGlobal && window.crmGlobal.doSearch();
        });        
      }else{
        Toast.fail(data.msgcode, 1);
      }      
    });
  }

  @action
  tabChange(page){
    this.tabPage = page;
    if(page == "contact"){
      this.getContactList();
    }
  }

  @action
  getContactList(){
    const params = {id:this.customerId};
    Apis.getContactListResult(params).then(data => { 
      const dataKey = data.datas;
      this.contactListStore.clearData();
      this.contactListStore.getData({ dataKey });
    });
  }
}